/*
 * @Description: noticeHooks
 * @Author: qingyang
 * @Date: 2023-05-30 15:21:53
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-03 15:51:48
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getMailList } from '@/apis/system/systemLog/logList'
import type { IMailParams } from '@/apis/system/systemLog/model/logListModel'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useLogList = () => {
  // 搜索字段及其他
  const searchForm = reactive<IMailParams>({
    type: '',
    isRead: undefined,
    title: undefined,
    startSendDate: undefined,
    endSendDate: undefined
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '通知标题/通知内容',
      field: 'title',
      type: 'input',
      style: 'width:300px'
    },
    {
      name: '通知类型',
      field: 'type',
      type: 'select',
      fixedOption: 'noticeTypeOptions'
    },
    {
      name: '阅读状态',
      field: 'isRead',
      type: 'select',
      fixedOption: 'receiveStatusOptions',
      style: 'width:140px'
    },
    {
      name: '发送日期',
      field: 'sendDate',
      type: 'datePicker',
      startPlaceholder: '发送日期开始',
      endPlaceholder: '发送日期截止',
      dateType: 'daterange',
      format: 'YYYY-MM-DD'
    }
  ])
  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData
  }
  function loadData(parameter: BasicPagingListParams) {
    const params: IMailParams & BasicPagingListParams = Object.assign(handleParams(), parameter)
    return getMailList(params)
  }
  // 处理参数
  const handleParams = (): IMailParams => {
    // 参数
    let params: IMailParams = {
      type: searchForm.type,
      isRead: searchForm.isRead === '' ? undefined : searchForm.isRead,
      title: searchForm.title,
      startSendDate: searchForm.sendDate?.length ? searchForm.sendDate[0] : undefined,
      endSendDate: searchForm.sendDate?.length ? searchForm.sendDate[1] : undefined
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '通知标题/通知内容',
      field: 'noticeTitle',
      slot: 'titleSlot',
      minWidth: '500',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '通知类型',
      field: 'type',
      slot: 'typeSlot',
      width: '100',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '接收人',
      field: 'receiveUsername',
      slot: 'receiveUsernameSlot',
      minWidth: '260',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '阅读状态',
      field: 'isRead',
      slot: 'isReadSlot',
      minWidth: '120',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '发送时间',
      field: 'sendTime',
      width: '180',
      columnSettingConfig: {
        enabled: true
      }
    }
  ]

  return {
    searchOptions,
    searchForm,
    tableOptions,
    tableFieldsSetting
  }
}
